//
//  ZXMessageModel.m
//  ZXDNLLTest
//
//  Created by mxsm on 16/5/18.
//  Copyright © 2016年 mxsm. All rights reserved.
//

#import "ZXMessageModel.h"
#import "ZXChatHelper.h"

static UILabel *label = nil;

@implementation ZXMessageModel


-(id)init
{
    if (self = [super init]) {
        
        if (label == nil) {
            label = [[UILabel alloc] init];
            [label setNumberOfLines:0];
            [label setFont:[UIFont systemFontOfSize:16.0f]];
        }
    }
    
    return self;
}

#pragma mark - Setter
-(void) setText:(NSString *)text
{
    _text = text;
    if (text.length > 0) {
        
        _attrText = [ZXChatHelper formatMessageString:text];
        
    }
}

#pragma mark - Getter
- (void) setMessageType:(ZXMessageType)messageType
{
    
    _messageType = messageType;
    switch (messageType) {
        case ZXMessageTypeText:
            self.cellIndentify = @"ZXTextMessageCell";
            break;
        case ZXMessageTypeImage:
            self.cellIndentify = @"ZXImageMessageCell";
            break;
//        case ZXMessageTypeVoice:
//            self.cellIndentify = @"ZXVoiceMessageCell";
//            break;
//        case ZXMessageTypeSystem:
//            self.cellIndentify = @"ZXSystemMessageCell";
//            break;
        default:
            break;
    }
}


-(CGSize) messageSize
{
    
    switch (self.messageType) {
            
        case ZXMessageTypeText:
            [label setAttributedText:self.attrText];
            _messageSize = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH * 0.58, MAXFLOAT)];
            break;
            
        case ZXMessageTypeImage:
        {
            _image = self.image;
            if (_image != nil) {
                _messageSize = (_image.size.width > SCREEN_WIDTH * 0.5 ? CGSizeMake(SCREEN_WIDTH * 0.5, SCREEN_WIDTH * 0.5 / _image.size.width * _image.size.height) : _image.size);
                _messageSize = (_messageSize.height > 60 ? (_messageSize.height < 200 ? _messageSize : CGSizeMake(_messageSize.width, 200)) : CGSizeMake(60.0 / _messageSize.height * _messageSize.width, 60));
            }
            else {
                _messageSize = CGSizeMake(SCREEN_WIDTH * 0.5, SCREEN_WIDTH * 0.5);
            }
            break;
        }
//        case ZXMessageTypeVoice:
//            break;
//            
//        case ZXMessageTypeSystem:
//            break;
            
        default:
            break;
    }
    
    return _messageSize;
}


-(CGFloat) cellHeight
{
    
    switch (self.messageType){
            // cell 上下间隔为10
        case ZXMessageTypeText:
            
            return self.messageSize.height + 65 > 85 ? self.messageSize.height + 65 : 85;
            break;
            
        case ZXMessageTypeImage:
            
            return self.messageSize.height + 45;
            break;
            
        default:
            
            break;
    }
    
    return 0;
}

- (UserModel *)from{
    if (!_from) {
        _from=[[UserModel alloc]init];
    }
    return _from;
}

+ (ZXMessageModel *)modelWithMessageDict:(NSDictionary *)dict{
    ZXMessageModel *model=[[ZXMessageModel alloc]init];
    
    int msgTypeChild =[[dict objectForKey:@"MsgTypeChild"] intValue];
    model.messageType=msgTypeChild;
    if (model.messageType==ZXMessageTypeText) {
        model.text=[dict objectForKey:@"Content"];
    }else if (model.messageType==ZXMessageTypePeopleCount){
        model.RoomManNum=[NSString stringWithFormat:@"%@",[dict objectForKey:@"RoomManNum"]];
    }else if (model.messageType==ZXMessageTypeImage){
        model.imageURL=[dict objectForKey:@"Content"];
    }
    model.BlackPeople=[NSString stringWithFormat:@"%@",[dict objectForKey:@"BlackPeople"]];
    model.Guid=[dict objectForKey:@"Guid"];
    model.from.UserId=[NSString stringWithFormat:@"%@",[dict objectForKey:@"UserId"]];
    model.from.MasterLottery=[dict objectForKey:@"MasterLottery"];
    if ([model.from.UserId isEqualToString:[UserModel userModel].UserId]) {
        model.ownerTyper=ZXMessageOwnerTypeSelf;
    }else{
        model.ownerTyper=ZXMessageOwnerTypeOther;
    }
    model.from.NickName=[dict objectForKey:@"UserName"];
    model.from.Avater=[dict objectForKey:@"PhotoImg"];
    model.isAdmin=[[dict objectForKey:@"IsAdmin"] boolValue];
//    model.from.isAdmin=[[dict objectForKey:@"IsAdmin"] boolValue];
    model.dateString=[dict objectForKey:@"SendTimeStr"];
    return model;
}

- (NSString *)cellIndentify{
    if (!_cellIndentify) {
        _cellIndentify=@"ZXTextMessageCell";
    }
    return _cellIndentify;
}

@end
